package com.pinyougou.sellergoods.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pinyougou.mapper.TbBrandMapper;
import com.pinyougou.pojo.TbBrand;
import com.pinyougou.pojo.TbBrandExample;
import com.pinyougou.pojo.TbBrandExample.Criteria;
import com.pinyougou.sellergoods.service.IBrandService;

import entity.PageResult;
@Service
public class BrandServiceImpl implements IBrandService {

	@Autowired
	private TbBrandMapper brandMapper;
	
	@Override
	public List<TbBrand> findAllBrand() {
		
		return brandMapper.selectByExample(null);
	}

	
	/**
	 * 分页查询所有品牌信息，并且包含模糊查询
	 * 
	 * @param brand		模糊查询的参数
	 * @param pageNum	当前页	
	 * @param pageSize	每页显示条数
	 * @return
	 */
	@Override
	public PageResult findPageAll(TbBrand brand, Integer pageNum, Integer pageSize) {
		//1、调用分页查询插件
		PageHelper.startPage(pageNum, pageSize);
		
		//2、判断模糊查询的参数是否为空
		TbBrandExample example = new TbBrandExample();
		if(brand!=null) {
			Criteria criteria = example.createCriteria();
			if (brand.getName()!=null && brand.getName().length()>0) {
				criteria.andNameLike("%"+brand.getName()+"%");
			}
			if (brand.getFirstChar()!=null && brand.getFirstChar().length()>0) {
				criteria.andFirstCharLike("%"+brand.getFirstChar()+"%");
			}
		}
		
		//3、查询数据
		Page<TbBrand> page = (Page<TbBrand>)brandMapper.selectByExample(example);
		
		return new PageResult(page.getTotal(), page.getResult());
	}


	/**
	 * 添加品牌信息
	 */
	@Override
	public void addBrand(TbBrand brand) {
	
		brandMapper.insert(brand);
	}


	/**
	 * 删除品牌信息
	 */
	@Override
	public void deleteBrand(Long[] ids) {
		for (Long id : ids) {
			brandMapper.deleteByPrimaryKey(id);
		}
	}


	/**
	 * 根据品牌ID查询品牌信息
	 * 
	 * @param id	品牌ID
	 * @return
	 */
	@Override
	public TbBrand findByBrandId(Long id) {
		return brandMapper.selectByPrimaryKey(id);
	}

	
	/**
	 * 修改品牌信息
	 * 
	 * @param brand	品牌信息
	 */
	@Override
	public void updateBrand(TbBrand brand) {
		brandMapper.updateByPrimaryKey(brand);
	}

	
	

}
